package com.prs.crm.dao.crm.impl;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.crm.CustomerDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAgent;
import com.prs.crm.domain.crm.CustomerSupplier;
import com.prs.crm.domain.crm.Liaison;
import com.prs.crm.domain.hr.Person;

public  class CustomerDaoImpl extends BaseDaoImpl<Customer> implements CustomerDao{

	@SuppressWarnings("unchecked")
	public 	Collection<Customer> getAllLiaisonCustomers()
	{
		return (Collection<Customer>) this.getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				Criteria criteria = arg0.createCriteria(Customer.class);
				criteria.add(Restrictions.isNotEmpty("liaisons"));
				return criteria.list();
			}
			
		});
	}
	
	@Override
	public Collection<?> getCustomerTypeReport(Date beginTime, Date endTime) {
		Criteria criteria = this.getSession().createCriteria(Customer.class);
		if(beginTime != null)
			criteria.add(Restrictions.ge("recordTime", beginTime));
		if(endTime != null)
			criteria.add(Restrictions.le("recordTime", endTime));
		
		ProjectionList projectionlist=Projections.projectionList();  
    	projectionlist.add(Projections.groupProperty("type"));
    	projectionlist.add(Projections.rowCount());
    	criteria.setProjection(projectionlist);
		return criteria.list();
	}

	@Override
	public Collection<?> getCustomerBusinessReport(Date beginTime, Date endTime) {
		Criteria criteria = this.getSession().createCriteria(Customer.class);
		if(beginTime != null)
			criteria.add(Restrictions.ge("recordTime", beginTime));
		if(endTime != null)
			criteria.add(Restrictions.le("recordTime", endTime));
		
		ProjectionList projectionlist=Projections.projectionList();  
    	projectionlist.add(Projections.groupProperty("trade"));
    	projectionlist.add(Projections.rowCount());
    	criteria.setProjection(projectionlist);
		return criteria.list();
	}

	@Override
	public Collection<?> getReportSouce(Date dates, Person person, Integer type, String merit) {
		Criteria criteria = this.getSession().createCriteria(Liaison.class);
		if(merit != null){
			criteria.createAlias("customer", "cu");
			criteria.add(Restrictions.eq("cu.merit", merit));
		}
		if(type == 1){
			if(person != null){
				criteria.add(Restrictions.eq("recorder", person));
			}
			if(dates != null){
				criteria.add(Restrictions.ge("liaisonTime", dates));
				Calendar c = Calendar.getInstance();
				c.setTime(dates);
				c.add(Calendar.YEAR, 1);
				criteria.add(Restrictions.lt("liaisonTime", c.getTime()));
			}
		}else{
			if(person != null){
				criteria.add(Restrictions.eq("recorder", person));
			}
			if(dates != null){
				criteria.add(Restrictions.ge("liaisonTime", dates));
				Calendar c = Calendar.getInstance();
				c.setTime(dates);
				c.add(Calendar.MONTH, 1);
				criteria.add(Restrictions.lt("liaisonTime", c.getTime()));
			}
		}
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Customer> getAllCustomer() {
		// TODO Auto-generated method stub
		return (Collection<Customer>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Customer.class);
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Customer> getCustomerByName(final String name) {
		// TODO Auto-generated method stub
		return (Collection<Customer>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Customer.class);
				criteria.add(Restrictions.eq("name", name));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Customer> getAllNoneLiaisonCustomers(final String tradeIds) {
		// TODO Auto-generated method stub
		return (Collection<Customer>) this.getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				Criteria criteria = arg0.createCriteria(Customer.class);
				criteria.add(Restrictions.isEmpty("liaisons"));
				if(tradeIds != null){
					if(!"all".equals(tradeIds)){
						String ids = tradeIds.split(",")[0];
						if(ToolKits.isInteger(ids)){
							criteria.createAlias("trade", "tr");
							criteria.add(Restrictions.eq("tr.id", Integer.parseInt(ids)));
						}
					}
				}
				return criteria.list();
			}
			
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Customer> getCustomerAddReportSouce(final Date dates, final Integer type) {
		// TODO Auto-generated method stub
		return (Collection<Customer>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
				Criteria criteria = arg0.createCriteria(Customer.class);
				if(type == 1){
					if(dates != null){
						criteria.add(Restrictions.ge("recordTime", dates));
						Calendar c = Calendar.getInstance();
						c.setTime(dates);
						c.add(Calendar.YEAR, 1);
						criteria.add(Restrictions.lt("recordTime", c.getTime()));
					}
				}else{
					if(dates != null){
						criteria.add(Restrictions.ge("recordTime", dates));
						Calendar c = Calendar.getInstance();
						c.setTime(dates);
						c.add(Calendar.MONTH, 1);
						criteria.add(Restrictions.lt("recordTime", c.getTime()));
					}
				}
				return criteria.list();
			}});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Customer> getCustomerByLay() {
		Criteria criteria=this.getSession().createCriteria(Customer.class);
		criteria.add(Restrictions.isNotNull("latlng"));
		return criteria.list();
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> getCustomerByAgent(final CustomerAgent customerAgent) {
		return (List<Customer>) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Customer.class);
				criteria.add(Restrictions.eq("customerAgent", customerAgent));
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> getCustomerBySupplier(
			final CustomerSupplier customerSupplier) {
		return (List<Customer>) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Customer.class);
				criteria.add(Restrictions.eq("customerSupplier", customerSupplier));
				return criteria.list();
			}
		});
	}

}
